package com.byb.server.service.impl;

import com.byb.server.service.PayRuleDescriptionService;
import com.byb.server.domain.PayRuleDescription;
import com.byb.server.repository.PayRuleDescriptionRepository;
import com.byb.server.repository.search.PayRuleDescriptionSearchRepository;
import com.byb.server.service.dto.PayRuleDescriptionDTO;
import com.byb.server.service.mapper.PayRuleDescriptionMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing PayRuleDescription.
 */
@Service
@Transactional
public class PayRuleDescriptionServiceImpl implements PayRuleDescriptionService{

    private final Logger log = LoggerFactory.getLogger(PayRuleDescriptionServiceImpl.class);

    private final PayRuleDescriptionRepository payRuleDescriptionRepository;

    private final PayRuleDescriptionMapper payRuleDescriptionMapper;

    private final PayRuleDescriptionSearchRepository payRuleDescriptionSearchRepository;
    public PayRuleDescriptionServiceImpl(PayRuleDescriptionRepository payRuleDescriptionRepository, PayRuleDescriptionMapper payRuleDescriptionMapper, PayRuleDescriptionSearchRepository payRuleDescriptionSearchRepository) {
        this.payRuleDescriptionRepository = payRuleDescriptionRepository;
        this.payRuleDescriptionMapper = payRuleDescriptionMapper;
        this.payRuleDescriptionSearchRepository = payRuleDescriptionSearchRepository;
    }

    /**
     * Save a payRuleDescription.
     *
     * @param payRuleDescriptionDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public PayRuleDescriptionDTO save(PayRuleDescriptionDTO payRuleDescriptionDTO) {
        log.debug("Request to save PayRuleDescription : {}", payRuleDescriptionDTO);
        PayRuleDescription payRuleDescription = payRuleDescriptionMapper.toEntity(payRuleDescriptionDTO);
        payRuleDescription = payRuleDescriptionRepository.save(payRuleDescription);
        PayRuleDescriptionDTO result = payRuleDescriptionMapper.toDto(payRuleDescription);
        //payRuleDescriptionSearchRepository.save(payRuleDescription);
        return result;
    }

    /**
     *  Get all the payRuleDescriptions.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<PayRuleDescriptionDTO> findAll(Pageable pageable) {
        log.debug("Request to get all PayRuleDescriptions");
        return payRuleDescriptionRepository.findAll(pageable)
            .map(payRuleDescriptionMapper::toDto);
    }

    /**
     *  Get one payRuleDescription by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public PayRuleDescriptionDTO findOne(Long id) {
        log.debug("Request to get PayRuleDescription : {}", id);
        PayRuleDescription payRuleDescription = payRuleDescriptionRepository.findOne(id);
        return payRuleDescriptionMapper.toDto(payRuleDescription);
    }

    /**
     *  Delete the  payRuleDescription by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete PayRuleDescription : {}", id);
        payRuleDescriptionRepository.delete(id);
        //payRuleDescriptionSearchRepository.delete(id);
    }

    /**
     * Search for the payRuleDescription corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<PayRuleDescriptionDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of PayRuleDescriptions for query {}", query);
        Page<PayRuleDescription> result = payRuleDescriptionSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(payRuleDescriptionMapper::toDto);
    }
}
